package com.spa.infrastructure.repository.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.spa.infrastructure.repository.condition.MassagistOnoffCondition;
import com.spa.infrastructure.repository.mapper.MassagistOnoffMapper;
import com.spa.infrastructure.repository.po.MassagistOnoffPO;
import com.spa.infrastructure.repository.po.MassagistPO;
import com.spa.infrastructure.repository.po.ShopPO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
@Data
public class MassagistOnoffRepositoryService extends MPJBaseServiceImpl<MassagistOnoffMapper, MassagistOnoffPO> {

    @Resource
    private MassagistOnoffMapper massagistOnoffMapper;


    private LambdaQueryWrapper<MassagistOnoffPO> wrapper(MassagistOnoffCondition condition) {
        LambdaQueryWrapper<MassagistOnoffPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(condition.getMassagistId()), MassagistOnoffPO::getMassagistId, condition.getMassagistId());
        wrapper.ge(ObjectUtil.isNotNull(condition.getBeginDate()), MassagistOnoffPO::getOnline, condition.getBeginDate());
        wrapper.le(ObjectUtil.isNotNull(condition.getEndDate()), MassagistOnoffPO::getOnline, condition.getEndDate());
        wrapper.isNull(condition.getNotOnline(),MassagistOnoffPO::getOffline);
        wrapper.orderByDesc(MassagistOnoffPO::getOnline);
        return wrapper;
    }

    private MPJLambdaWrapper<MassagistOnoffPO> joinMassagistShopWrapper(MassagistOnoffCondition condition) {
        MPJLambdaWrapper<MassagistOnoffPO> wrapper = JoinWrappers.lambda(MassagistOnoffPO.class)
                .selectAll(MassagistOnoffPO.class)//查询表全部字段
                .selectAs(ShopPO::getName,"shopName")
                .selectAs(MassagistPO::getName,"name")
                .selectAs(MassagistPO::getPhone,"phone")
                .selectAs(MassagistPO::getWorkPhoto,"workPhoto")
                .leftJoin(MassagistPO.class, MassagistPO::getId, MassagistOnoffPO::getMassagistId)
                .leftJoin(ShopPO.class, ShopPO::getId, MassagistPO::getShopId);
        wrapper.ge(ObjectUtil.isNotNull(condition.getBeginDate()), MassagistOnoffPO::getOnline, condition.getBeginDate());
        wrapper.le(ObjectUtil.isNotNull(condition.getEndDate()), MassagistOnoffPO::getOnline, condition.getEndDate());
        wrapper.eq(ObjectUtil.isNotNull(condition.getShopId()), ShopPO::getId, condition.getShopId());
        wrapper.eq(MassagistPO::getIsDel, 0);
        return wrapper;
    }

    public List<MassagistOnoffPO> queryList(MassagistOnoffCondition condition) {
        return list(wrapper(condition));
    }

    public Page<MassagistOnoffPO> page(MassagistOnoffCondition condition) {
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper(condition));
    }

    public Page<MassagistOnoffPO> pageJoin(MassagistOnoffCondition condition) {
        return page(Page.of(condition.getCurrent(), condition.getSize()), joinMassagistShopWrapper(condition));
    }

    public List<MassagistOnoffPO> getNotOff(MassagistOnoffCondition condition) {
        condition.setNotOnline(true);
        return list(wrapper(condition));
    }

    public void deleteNotOff(MassagistOnoffCondition condition) {
        condition.setNotOnline(true);
        remove(wrapper(condition));
    }

    public List<MassagistOnoffPO> onffList(MassagistOnoffCondition condition) {
        return list(joinMassagistShopWrapper(condition));
    }
}
